Showing posts with label Image Processing. Show all posts
Showing posts with label Image Processing. Show all posts

Thursday, April 30, 2015

Rotate Image Algorithm matlab implementation

function [R] = RotateIm(A,angle)
A=padarray(A,[50,50]);
angle=-angle*pi/180; %to make the positive rotate clock wise like required
Tinv=[cos(angle) sin(angle);-sin(angle) cos(angle)];
R=zeros(size(A,1),size(A,2));
px=(size(A,1))/2;
py=(size(A,2))/2;
for m = 1:size(A,1)-1
    for n = 1:size(A,2)-1
        x=Tinv*[m-px;n-py]+[px;py];
        I=floor((x(1,1)));
        K=floor((x(2,1)));
        a=x(1,1)-I;
        b=x(2,1)-K;
       
       
       %  R(m,n)=R(m,n)+(1-a)*(1-b)*A(I,K)+a*(1-b)*A(I+1,K)+(1-a)*b*A(I,K+1)+a*b*A(I+1,K+1);
         if(I>=1&&I<=size(A,1)&&K<=size(A,2) && K >=1 )
             R(m,n)=R(m,n)+(1-a)*(1-b)*A(I,K);
         end
        if(I<size(A,1)&&I>=1 && K >=1 && K <= size(A,2))
          R(m,n)=R(m,n)+  a*(1-b)*A(I+1,K);
        end
       
        if(I>=1 && I<=size(A,1) && K >=1&& K < size(A,2) )
          R(m,n)=R(m,n)+   (1-a)*b*A(I,K+1);
        end
        if(I<size(A,1) &&I>=1&&K>1&& K < size(A,2) )
              R(m,n)=R(m,n)+   a*b*A(I+1,K+1);
        end
    end
end
[MaxRow,MaxCol] = find(R,1,'last');

[MinRow,MinCol] = find(R,1,'first');

R=R(min(MinRow,MinCol):max(MaxRow,MaxCol),min(MinRow,MinCol):max(MaxRow,MaxCol));

R=uint8(R);

end

Integral Image Algorithm

function [II] = IntegIm(A)
A=double(A);
s=double(zeros(size(A,1)+1,size(A,2)+1));
II=double(zeros(size(A,1)+1,size(A,2)+1));
for  m = 2:size(A,1)+1
    for n = 2:size(A,2)+1
   
          s(m,n)=double(s(m,n-1) + A(m-1,n-1));
   
    end
   
end
for m = 2:size(s,1)
    for n = 2:size(s,2)
     II(m,n)=double(II(m-1,n)+s(m,n));
    end
end


end

Tuesday, April 21, 2015

Matlab Implementation for SIFT "Scale-invariant feature transform "

Scale-invariant feature transform (or SIFT) is an algorithm in computer vision to detect and describe local features in images.




function [outImage,keypoints] = my_sift(A,maxS,t)
if (~exist('t', 'var'))
        t=0;
end
 if(size(A, 3)>1)
A=rgb2gray(A);
end
m=1;
sigmas=(1:1:maxS);
%A=padarray(A,[1,1]);
A=double(A);
keypoints=zeros(size(A,1)*size(A,2),4);
logImages=zeros(size(A,1),size(A,2),size(sigmas,2));
t=t*max(max(A));
for k=1 :size(sigmas,2)
    h=sigmas(1,k)*sigmas(1,k)* fspecial('log', 2*ceil(3*sigmas(1,k))+1,sigmas(1,k));
     logImages(:,:,k)= imfilter(A,h);
end

for i=2 :size(A,1)-1,
    for j=2:size(A,2)-1,
       isKeypoint=1;
        [maxVal,maxInd]=max(abs(logImages(i,j,:)));
        if(maxInd~=1 && maxInd~=maxS)
         
           for q=(i-1):i+1
               for w=(j-1):j+1
                 
                 if(abs(logImages(i,j,maxInd))<(t*maxInd*maxInd)|| abs(logImages(i,j,maxInd))< abs(logImages(q,w,maxInd-1))||abs(logImages(i,j,maxInd))< abs(logImages(q,w,maxInd))||abs(logImages(i,j,maxInd))< abs(logImages(q,w,maxInd+1)))
                     isKeypoint=0;
                     break;
                 end
               end
                if(isKeypoint==0)
                    break;
                end
           end
        else
            isKeypoint=0;
        end
   
        if(isKeypoint==1)
            keypoints(m,1)=j;
             keypoints(m,2)=i;
              keypoints(m,3)=sqrt(2)*sigmas(1,maxInd);
              keypoints(m,4)=sigmas(1,maxInd);
             m=m+1;
                 
           
        end
   end
     
   
end
%yellow = uint8([255 255 0]);
%shapeInserter = vision.ShapeInserter('Shape','Circles','BorderColor','Custom','CustomBorderColor',yellow,'LineWidth',0.1);
A=uint8(A);
outImage=A;
keypoints= keypoints(1:min(find(keypoints==0))-1,1:4);
%RGB = repmat(A(2:size(A,1)-1,2:size(A,1)-1),[1,1,3]); % convert I to an RGB image
for b=1:size(keypoints,1)  %min(find(keypoints==min(keypoints,1)))
outImage = insertShape(outImage, 'circle',keypoints(b,1:3) );


end
%yellow = uint8([255 255 0]);
%shapeInserter = vision.ShapeInserter('Shape','Circles','BorderColor','white');
%RGB = repmat(A,[1,1,3]);
%outImage = step(shapeInserter, A(2:size(A,1)-1,2:size(A,1)-1), keypoints);

end

Saturday, April 18, 2015

Matlab Implementation For Average Filter Using Integral Image

function [Filtered_image] = AverageFilter(A,s)
%S should be odd if s is even will increment by 1
%size of the filter is sxs

if(rem(s,2)==0 && s>1)
   s=s+1;
end  
       

II=integralImage(A);

x=zeros(size(II,1),size(II,2));
for m = 2:size(A,1)
    for n = 2:size(A,2)
       
        if((m+floor(s/2))<=size(II,1)&&(n+floor(s/2))<=size(II,2))
          x(m,n)=x(m,n)+II(m+floor(s/2),n+floor(s/2));
        end
        if((n-floor(s/2)-1)>=1&&(m-floor(s/2)-1)>=1)
         x(m,n)=x(m,n)+II(m-floor(s/2)-1,n-floor(s/2)-1);
        end  
        if((m-floor(s/2)-1)>=1&&(n+floor(s/2))<=size(II,2))
             x(m,n)=x(m,n)-II(m-floor(s/2)-1,n+floor(s/2));  
        end
        if((m+floor(s/2))<=size(II,1)&&(n-floor(s/2)-1)>=1)
             x(m,n)=x(m,n)-II(m+floor(s/2),n-floor(s/2)-1);
        end
       
       x(m,n)=round((x(m,n)/(s*s)));
       
       
    end
end

 Filtered_image= uint8(x(2:size(x,1),2:size(x,2)));
end